<?php

class UpdateCommand extends CConsoleCommand {

    public function run($args) {

	/*
	 * send mail to users have some expired ads
	 */
//	$this->_sendMailToExpiredAds();

	/*
	 * send mail to eblast
	 */
//	$this->_sendMailEblast();
	/*
	 * send mail for re-active featured
	 */
	$this->_sendMailActiveBeforeWeek();
    }

    /*
     * send mail to users have some expired ads
     */

    private function _sendMailToExpiredAds() {
	//get all ads expire and have not sent yet
	$model = Ad::model()->with('user')->findAll(array(
	    'condition' => 't.expire_time<=:time and t.is_send_mail=:is_send_mail and t.status=:status',
	    'params' => array(
		':time' => time(),
		':is_send_mail' => Ad::NOT_SEND_MAIL,
		':status' => Ad::STATUS_APPROVE
	    )
	));
	$i = 0;
	foreach ($model as $item) {
	    if ($i == 100) {
		$i = 0;
		sleep(30);
	    }
	    $urlActive = "<a href='" . SITE_NAME_URL . 'ad/re_active/id/' . $item->ad_id . "'> here </a>";
	    EmailHelper::sendReactive($item->user->email, $item->user->username, $item->ad_id, $urlActive, '', $item->title);
	    //update to database is send
	    $item->is_send_mail = Ad::SENDED_MAIL;
	    $item->save();
	}
    }

    /*
     * send mail eblast to people have posts based on eblast's categories 
     */

    private function _sendMailEblast() {
	//get list eblast
	$model = Eblast::model()->with(array('categories', 'ad'))->findAll(array(
	    'condition' => 'UNIX_TIMESTAMP(t.start_date)<=:time and t.is_send_mail=:is_send_mail',
	    'params' => array(
		':time' => time(),
		':is_send_mail' => Ad::NOT_SEND_MAIL
	    )
	));

	foreach ($model as $itemEblast) {
	    //get list category of eblast
	    $sCat = $itemEblast->main_category;

	    foreach ($itemEblast->categories as $cat) {
		$sCat .= ', ' . $cat->category_id;
	    }

	    //get list user have posted to these categories
	    $aMail = Yii::app()->db->createCommand('
	    SELECT u.email, u.username from users u join 
	    (select a.user_id from ads a right 
		join ads_categories ac on a.ad_id=ac.ad_id 
		WHERE a.main_category IN (' . $sCat . ') and ac.category_id IN (' . $sCat . ') and 
		    a.`status` = ' . Ad::STATUS_APPROVE . ' GROUP BY a.user_id
	    ) as p
	    On u.user_id = p.user_id WHERE u.user_id != ' . $itemEblast->ad->user_id)->queryAll();
	    //send mail to these user
	    $i = 0;
	    foreach ($aMail as $itemMail) {
		if ($i == 100) {
		    $i = 0;
		    sleep(30);
		}
		EmailHelper::sendEblast($itemMail['email'], $itemMail['username'], $itemEblast->ad_id, $itemEblast->ad->content, $itemEblast->advertisement, $itemEblast->ad->contact_email, $itemEblast->ad->contact_phone, $itemEblast->ad->contact_name);
	    }
	}
    }

    /**
     * send mail before 1 weeks
     */
    private function _sendMailActiveBeforeWeek() {
	$timeBefore = time() + 7 * 24 * 60 * 60;
	//get all ads expire and have not sent yet
	$model = Ad::model()->with('user')->findAll(array(
	    'condition' => 't.expire_time<=:time and t.is_send_mail_before=:is_send_mail_before and t.status=:status',
	    'params' => array(
		':time' => $timeBefore,
		':is_send_mail_before' => Ad::NOT_SEND_MAIL,
		':status' => Ad::STATUS_APPROVE
	    )
	));
	$i = 0;
	foreach ($model as $item) {
	    if ($i == 100) {
		$i = 0;
		sleep(30);
	    }
	    $urlActive = "<a href='" . SITE_NAME_URL . 'ad/re_active/id/' . $item->ad_id . "'> here </a>";
	    EmailHelper::sendReactiveBefore($item->user->email, $item->user->username, $item->ad_id, $urlActive, '', $item->title);
	    //update to database is send
	    $item->is_send_mail_before = Ad::SENDED_MAIL;
	    $item->save();
	}
    }

}

